package cn.itcast.store.dao.daoImp;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import cn.itcast.store.dao.UserDao;
import cn.itcast.store.domain.Admin;
import cn.itcast.store.domain.User;
import cn.itcast.store.utils.JDBCUtils;

public class UserDaoImp implements UserDao {

	@Override
	public void userRegist(User user01) throws SQLException {
		String sql="INSERT INTO USER VALUES(?,?,?,?,?,?,?,?,?,?)";
		Object[] params={user01.getUid(),user01.getUsername(),user01.getPassword(),user01.getName(),user01.getEmail(),user01.getTelephone(),user01.getBirthday(),user01.getSex(),user01.getState(),user01.getCode()};
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		System.out.println(user01);
		qr.update(sql,params);
	}

	@Override
	public User userActive(String code) throws Exception {
		String sql="select * from user where code =?";
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		return qr.query(sql, new BeanHandler<User>(User.class),code);
	}

	@Override
	public void updateUser(User user) throws Exception {
		String sql="update user set username= ? ,password=? ,name =? ,email =? ,telephone =? , birthday =?  ,sex =? ,state= ? , code = ? WHERE uid=?";
		Object[] params={user.getUsername(),user.getPassword(),user.getName(),user.getEmail(),user.getTelephone(),user.getBirthday(),user.getSex(),user.getState(),user.getCode(),user.getUid()};
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		qr.update(sql,params);
		
	}

	@Override
	public User login(User user) throws Exception {
		String sql="select * from user where username =? and password=? and state=1";
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		return qr.query(sql, new BeanHandler<User>(User.class),user.getUsername(),user.getPassword());
		
	}

	@Override
	public Admin Adminlogin(Admin admin) throws Exception {
		String sql="select * from admin where username =? and password=?";
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		return qr.query(sql, new BeanHandler<Admin>(Admin.class),admin.getUsername(),admin.getPassword());
	}

}
